<?php

class Crud_Form_Element_Column_ForeignKey extends Crud_Form_Element_Column
{
	public function init()
	{
		//$this->_initRefreshDecorator();
		//$this->_initSuggestDecorator();
		$this->initMultiOptions();
	}
	
	/**
	 * Adding refresh decorator for element.
	 * 
	 * @return void
	 */
	protected function _initRefreshDecorator()
	{
		$column = $this->getColumn();
		$table = $column->getFKTable()->getTableName();
		$url = "/admin/crud/list/table/$table";
		$this->getElement()->addDecorator('refresh', array('url' => $url));		
	}
	
	protected function _initSuggestDecorator()
	{
		$element = $this->getElement();
		$element->addDecorator('suggest');
	}
	
	/**
	 * Setup default values for element.
	 * 
	 * @param string $filter Suggest filter for getFKValues.
	 * @return void
	 */
	public function initMultiOptions($filter = null)
	{
		if ($filter !== null) {
			$this->getElement()->removeDecorator('suggest');
		}
		
		$element = $this->getElement();
		$column = $this->getColumn();
		$values = $column->getFKValues(null, $filter);
		
		$element->clearMultiOptions();
		if ($column->isNull()) {
			$element->addMultiOption(null);
		}
		
		$element->addMultiOptions($values);
	}
	
	/**
	 * Return element class name by column type.
	 * Try to get class from config or automatic from column type.
	 * 
	 * @return string
	 */
	protected function _getElementClass()
	{
		return 'Zend_Form_Element_Select';
	}
}